﻿using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;
using System.Linq;

namespace Cxuu.Models
{
    public static partial class CategoryExtensions
    {
        /// <summary>
        /// 递归遍历栏目列表并将其排序
        /// </summary>
        /// <param name="article_Cates"></param>
        /// <param name="treeModels"></param>
        /// <param name="parentId"></param>
        /// <param name="depth"></param>
        public static void IndentedCate(IEnumerable<ArticleCate> article_Cates, IList<ViewArticleCate> treeModels, int parentId = 0, int depth = 0)
        {
            depth++;
            List<ArticleCate> cates = article_Cates.Where(a => a.Pid == parentId).ToList();
            foreach (ArticleCate item in cates)
            {
                ViewArticleCate treeModel = new()
                {
                    Id = item.Id,
                    Title = item.Name,
                    Type = item.Type,
                    Theme = item.Theme,
                    Num = item.Num,
                    Sort = item.Sort,
                    Depth = depth,
                    Spread = "true",
                };
                if (item.Type == true)
                    treeModel.Disabled = "true";
                treeModels.Add(treeModel);
                IndentedCate(article_Cates, treeModel.Children, treeModel.Id, depth);
            }
        }
        /// <summary>
        /// 转换栏目列表模型
        /// </summary>
        /// <param name="article_Cates"></param>
        /// <returns></returns>
        public static IEnumerable<ViewArticleCate> AsViewArticleCates(this IEnumerable<ArticleCate> article_Cates)
        {
            List<ViewArticleCate> treeModels = new();
            IndentedCate(article_Cates, treeModels);
            
            List<ViewArticleCate> outModels = new();
            foreach (var item in treeModels)
            {
                MegCate(outModels, item);
            }
            return outModels;
        }

        static void MegCate(IList<ViewArticleCate> outModels, ViewArticleCate cate)
        {
            outModels.Add(cate);
            foreach (var item in cate.Children)
            {
                item.ChildrenCount = item.Children.Count;
                MegCate(outModels, item);
            }
        }

        /// <summary>
        /// selectlist 引用
        /// </summary>
        /// <param name="article_Cates"></param>
        /// <returns></returns>
        public static IEnumerable<SelectListItem> AsSelectListItems(this List<ArticleCate> article_Cates)
        {
            List<SelectListItem> valueCategorys = new();
            IEnumerable<ViewArticleCate> categories = article_Cates.AsViewArticleCates();
            for (int i = 0; i < categories.Count(); i++)
            {
                var folder = categories.ElementAt(i);
                var headString = "";
                if (folder.Depth > 1)
                {
                    if (folder.ChildrenCount > 0)
                        headString += "├";
                    else
                        headString += "└";
                    for (int j = 1; j < folder.Depth; j++)
                        headString += " ─ ";
                }
                var selecttext = string.Format("{0}{1}", headString, folder.Title);
                if (folder.Type == true)
                    valueCategorys.Add(new SelectListItem { Text = selecttext, Value = folder.Id.ToString(), Disabled = true });
                else
                    valueCategorys.Add(new SelectListItem { Text = selecttext, Value = folder.Id.ToString(), Disabled = false });
            }
            return valueCategorys;
        }

        /// <summary>
        /// 栏目列表 引用
        /// </summary>
        /// <param name="article_Cates"></param>
        /// <returns></returns>
        public static IEnumerable<ViewArticleCate> AsCateListItems(this List<ArticleCate> article_Cates)
        {
            List<ViewArticleCate> cate = new();
            IEnumerable<ViewArticleCate> categories = article_Cates.AsViewArticleCates();
            for (int i = 0; i < categories.Count(); i++)
            {
                ViewArticleCate folder = categories.ElementAt(i);
                string headString = "";
                if (folder.Depth > 1)
                {
                    if (folder.ChildrenCount > 0)
                        headString += "├";
                    else
                        headString += "└";
                    for (int j = 1; j < folder.Depth; j++)
                        headString += "─";
                }
                string selecttext = string.Format("{0}{1}", headString, folder.Title);
                cate.Add(new ViewArticleCate { Id = folder.Id, Pid = folder.Pid, Title = selecttext, Sort = folder.Sort, Theme = folder.Theme, Type = folder.Type, Num = folder.Num, Depth = folder.Depth });

            }
            return cate.ToList();//.OrderBy(o => o.Sort).ThenBy(o => o.Depth).ToList();//排序
        }
    }
}
